package com.hall.controller;

import com.hall.pojo.Appointment;
import com.hall.pojo.SysUser;
import com.hall.pojo.common.Result;
import com.hall.service.AttentionServes;
import com.hall.service.UserService;
import com.hall.utils.UserSession;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
/**
 *关注收藏
 */
@Controller
@RequestMapping("/my")
public class AttentionController {
    @Autowired
    private AttentionServes attentionServes;
    @Autowired
    private UserService userService;
    //关注和收藏列表
    @GetMapping("/attention")
    public String myAttention(HttpServletRequest request, Model m2, HttpSession session) {
        SysUser sysUser = UserSession.getSysUser(request);
        SysUser s = userService.getUser(sysUser.getId());
        System.out.println("------------------------" + s.getId());
        List<SysUser> list3 = attentionServes.findAttentionById(s.getId(), 1,null);
        List<Appointment> list4 = attentionServes.findCollectById(s.getId(), 1);
        m2.addAttribute("Attent", list3);
        m2.addAttribute("Collect", list4);
        return "attention";
    }
    //当前登录用户对象的关注列表
    @ResponseBody()
    @GetMapping("/getAll")
    public Result<List<SysUser>>getAll(HttpServletRequest request){
        SysUser sysUser = UserSession.getSysUser(request);
        List<SysUser> list3 = attentionServes.findAttentionById(sysUser.getId(), 1,null);
        return Result.success(list3,"请求成功");
    }
    //当前登录用户对象的关注列表(姓名检索)
    @ResponseBody
    @PostMapping("/getList")
    public Result<List<SysUser>>getList(@RequestBody SysUser user, HttpServletRequest request){
        System.out.println("00000000000000000000"+user.getName());
        SysUser sysUser = UserSession.getSysUser(request);
        List<SysUser> list3 = attentionServes.findAttentionById(sysUser.getId(), 1,user.getName());
        return Result.success(list3,"请求成功");
    }
    //当前登录用户粉丝列表
    @ResponseBody
    @GetMapping("/getFansList")
    public Result<List<SysUser>>getFansList(HttpServletRequest request){
        String id = UserSession.getSysUser(request).getId();
        List<SysUser>listFand = attentionServes.findFansList(id);
        if(listFand.size()<=0){
            return Result.success(null,"暂没有粉丝哦");
        }else{
            return Result.success(listFand,"获取成功");
        }
    }
    //关注与收藏的检索(旧版)
    @GetMapping("/attention/{index}")
    public String getByIndex(@PathVariable String index,HttpServletRequest request,Model m3){
        SysUser sysUser = UserSession.getSysUser(request);
        if(sysUser.getId()==null){
            return "return_login";
        }
        SysUser s = userService.getUser(sysUser.getId());
        List<SysUser> list3 = attentionServes.selectAttention(s.getId(), 1,index); //匹配的用户
        List<Appointment> list4 = attentionServes.selectCollect(s.getId(), 1,index); //匹配的会议
        m3.addAttribute("Attent", list3);
        m3.addAttribute("Collect", list4);
        return "attention";
    }
    //取关
    @ResponseBody
    @RequestMapping("/cancelAttention")
    public String cancelAttention(String id,HttpSession session,HttpServletRequest request) {
        SysUser sysUser = UserSession.getSysUser(request);
        System.out.println("-----------------" + id);
        int i = attentionServes.cancelAttentionById(0, id,sysUser.getId()); //isdel字段默认为1,改为0则表示取关
        if (i > 0)
            return "ok";
        else
            return "false";
    }

    //取消收藏
    @ResponseBody
    @RequestMapping("/cancelCollect")
    public String cancelCollect(String id,HttpSession session,HttpServletRequest request) {
        System.out.println("-----------------" + id);
        SysUser sysUser = UserSession.getSysUser(request);
        int i = attentionServes.cancelCollectById(0, id,sysUser.getId()); //isdel字段建表默认为1,改为0则表示取收藏
        if (i > 0)
            return "ok";
        else
            return "false";
    }
    //新版关注页面 /*未启用*/
    @RequestMapping("/attentions")
    public String attentionsPage(){
        return "user_fans";
    }

    //添加关注的用户
    @ResponseBody
    @PostMapping("/addAttention")
    public Result addAttention(String userid,String beUserid){
       return attentionServes.addAttention(userid,beUserid);
    }
}
